package com.ljf.framework;

import com.ljf.framework.Async.AsyncService;
import com.ljf.framework.Async.AsyncServiceDefaultImpl;
import com.ljf.framework.cache.LjfCacheService;
import com.ljf.framework.cache.LjfCacheServiceDefaultImpl;
import com.ljf.framework.context.LjfContext;
import com.ljf.framework.context.LjfContextDefaultImpl;
import com.ljf.framework.context.second.LjfSecondContext;
import com.ljf.framework.job.LjfJobService;
import com.ljf.framework.job.LjfJobServiceDefaultImpl;
import com.ljf.framework.password.PasswordEncoderService;
import com.ljf.framework.password.PasswordEncoderServiceDefaultImpl;
import com.ljf.framework.security.LjfSecurityService;
import com.ljf.framework.security.LjfSecurityServiceDefaultImpl;
import com.ljf.framework.security.token.LjfTokenConfig;
import com.ljf.framework.security.token.LjfTokenConfigFactory;
import com.ljf.framework.security.token.LjfTokenService;
import com.ljf.framework.security.token.LjfTokenServiceDefaultImpl;
import com.ljf.framework.util.json.LjfJsonService;
import com.ljf.framework.util.json.LjfJsonServiceDefaultImpl;

import java.util.concurrent.CompletableFuture;

/**
 * 说明：ljf框架的主管理类，可管理所有的功能和服务
 *
 * @Auther: lijinfeng
 * @Date: 2024/4/9
 */
public class LjfManager {
    /**
     * 安全接口
     */
    private static volatile LjfSecurityService ljfSecurityService;

    public static void setLjfSecurityService(LjfSecurityService ljfSecurityService) {
        LjfManager.ljfSecurityService = ljfSecurityService;
    }
    public static LjfSecurityService getLjfSecurityService() {
        if (ljfSecurityService == null) {
            synchronized (LjfManager.class) {
                if (ljfSecurityService == null) {
                    setLjfSecurityService(new LjfSecurityServiceDefaultImpl());
                }
            }
        }
        return ljfSecurityService;
    }

    /**
     * 数缓存接口
     */
    private static volatile LjfCacheService ljfCacheService;

    public static void setLjfCacheService(LjfCacheService ljfCacheService) {
        LjfManager.ljfCacheService = ljfCacheService;
    }
    public static LjfCacheService getLjfCacheService() {
        if (ljfCacheService == null) {
            synchronized (LjfManager.class) {
                if (ljfCacheService == null) {
                    setLjfCacheService(new LjfCacheServiceDefaultImpl());
                }
            }
        }
        return ljfCacheService;
    }


    /**
     * 密码加密接口
     */
    private static volatile PasswordEncoderService passwordEncoderService;

    public static void setPasswordEncoderService(PasswordEncoderService passwordEncoderService) {
        LjfManager.passwordEncoderService = passwordEncoderService;
    }
    public static PasswordEncoderService getPasswordEncoderService() {
        if (passwordEncoderService == null) {
            synchronized (LjfManager.class) {
                if (passwordEncoderService == null) {
                    setPasswordEncoderService(new PasswordEncoderServiceDefaultImpl());
                }
            }
        }
        return passwordEncoderService;
    }

    /**
     * token接口
     */
    private static volatile LjfTokenService ljfTokenService;

    public static void setLjfTokenService(LjfTokenService ljfTokenService) {
        LjfManager.ljfTokenService = ljfTokenService;
    }
    public static LjfTokenService getLjfTokenService() {
        if (ljfTokenService == null) {
            synchronized (LjfManager.class) {
                if (ljfTokenService == null) {
                    setLjfTokenService(new LjfTokenServiceDefaultImpl());
                }
            }
        }
        return ljfTokenService;
    }
    /**
     * 临时上下文Context 用完需要清空
     */
    private volatile static LjfContext ljfTempContext;
    public static void setLjfTempContext(LjfContext ljfContext) {
        LjfManager.ljfTempContext = ljfContext;
    }
    public static LjfContext getLjfTempContext() {
        return ljfTempContext;
    }
    /**
     * 上下文Context Bean
     */
    private volatile static LjfContext ljfContext;
    public static void setLjfContext(LjfContext ljfContext) {
        LjfManager.ljfContext = ljfContext;
    }
    public static LjfContext getLjfContext() {
        return ljfContext;
    }

    /**
     * 二级Context
     */
    private volatile static LjfSecondContext ljfSecondContext;
    public static void setLjfSecondContext(LjfSecondContext ljfSecondContext) {
        LjfManager.ljfSecondContext = ljfSecondContext;
    }
    public static LjfSecondContext getLjfSecondContext() {
        return ljfSecondContext;
    }

    /**
     * 获取一个可用的LjfContext
     * @return /
     */
    public static LjfContext getLjfContextOrSecond() {

        // s1. 一级Context可用时返回一级Context
        if (ljfContext != null) {
            if (ljfSecondContext == null && ljfContext.isValid()) {
                // 因为 isValid 是一个耗时操作，所以此处假定：二级Context为null的情况下无需验证一级Context有效性
                // 这样可以提升6倍左右的上下文获取速度
                return ljfContext;
            }
        }

        // s2. 一级Context不可用时判断二级Context是否可用
        if (ljfSecondContext != null && ljfSecondContext.isValid()) {
            return ljfSecondContext;
        }

        // 临时上下文，用完需要清空
        if (ljfTempContext != null ) {
            return ljfTempContext;
        }

        // s3. 都不行，就返回默认的 Context
        return LjfContextDefaultImpl.defaultContext;
    }

    public volatile static LjfTokenConfig ljfTokenConfig;
    public static void setLjfTokenConfig(LjfTokenConfig ljfTokenConfig) {
        setConfigMethod(ljfTokenConfig);

        // 打印 banner
        if(ljfTokenConfig !=null && ljfTokenConfig.getIsPrint()) {
            // 打印 banner
        }

        // $$ 全局事件
//        SaTokenEventCenter.doSetConfig(ljfTokenConfig);

        // 调用一次StpUtil中的方法，保证其可以尽早的初始化 StpLogic
//        StpUtil.getLoginType();
    }
    private static void setConfigMethod(LjfTokenConfig config) {
        LjfManager.ljfTokenConfig = config;
    }
    public static LjfTokenConfig getLjfTokenConfig() {
        if (ljfTokenConfig == null) {
            synchronized (LjfManager.class) {
                if (ljfTokenConfig == null) {
                    LjfManager.ljfTokenConfig= LjfTokenConfigFactory.createConfig();
                }
            }
        }
        return ljfTokenConfig;
    }

    /**
     * json 工具类，使用第三方，自己写的总有bug，还是用人家成熟的吧。。。。。。。。
     */
    private static volatile LjfJsonService ljfJsonService;

    public static void setLjfJsonService(LjfJsonService ljfJsonService) {
        LjfManager.ljfJsonService = ljfJsonService;
    }
    public static LjfJsonService getLjfJsonService() {
        if (ljfJsonService == null) {
            synchronized (LjfManager.class) {
                if (ljfJsonService == null) {
                    setLjfJsonService(new LjfJsonServiceDefaultImpl());
                }
            }
        }
        return ljfJsonService;
    }

    /**
     * 异步服务
     *
     * @return
     */
    private static volatile AsyncService asyncService;

    public static AsyncService getAsyncService() {
        if (asyncService == null) {
            synchronized (LjfManager.class) {
                if (asyncService == null) {
                    setAsyncService(new AsyncServiceDefaultImpl());
                }
            }
        }
        return asyncService;
    }

    public static void setAsyncService(AsyncService asyncService) {
        LjfManager.asyncService = asyncService;
    }

    /**
     * 定时任务服务
     *
     * @return
     */
    private static volatile LjfJobService ljfJobService;

    public static LjfJobService getLjfJobService() {
        if (ljfJobService == null) {
            synchronized (LjfManager.class) {
                if (ljfJobService == null) {
                    setLjfJobService(new LjfJobServiceDefaultImpl());
                }
            }
        }
        return ljfJobService;
    }

    public static void setLjfJobService(LjfJobService ljfJobService) {
        LjfManager.ljfJobService = ljfJobService;
    }
}
